package com.bobi.ecofavor.controller;

import com.bobi.ecofavor.common.enums.CacheKeyEnum;
import com.bobi.ecofavor.common.enums.ResponseCodeEnum;
import com.bobi.ecofavor.common.utils.RedisUtil;
import com.bobi.ecofavor.model.domain.User;
import com.bobi.ecofavor.model.dto.CouponDto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bobi.ecofavor.common.utils.JsonResponse;
import com.bobi.ecofavor.service.CouponService;
import com.bobi.ecofavor.model.domain.Coupon;

import java.util.List;

/**
 *
 *  前端控制器
 *
 *
 * @author wenyuan
 * @since 2023-10-21
 * @version v1.0
 */
@RestController
@RequestMapping("/api/coupon")
public class CouponController {

    private final Logger logger = LoggerFactory.getLogger( CouponController.class );

    @Autowired
    private CouponService couponService;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 查询用户未使用的优惠券
     */
    @RequestMapping("/getUnusedCoupons")
    public JsonResponse getUnusedCoupons() {
        User user = (User) redisUtil.get(CacheKeyEnum.USER.getKey());
        JsonResponse<List<CouponDto>> success = JsonResponse.success(couponService.getUnsedCoupons(user.getId()));
        return success;
    }

    /**
     * 查询用户历史优惠券
     * @return
     */
    @RequestMapping("/getPastCoupons")
    public JsonResponse getPastCoupons() {
        User user = (User) redisUtil.get(CacheKeyEnum.USER.getKey());
        JsonResponse pastCoupons = couponService.getPastCoupons(user.getId());
        return pastCoupons;
    }

    /**
     * 查询用户历史优惠券
     * @return
     */
    @RequestMapping("/getAllUnacceptedCoupons")
    public JsonResponse getAllUnacceptedCoupons() {
        User user = (User) redisUtil.get(CacheKeyEnum.USER.getKey());
        if (user == null) {
            JsonResponse<Object> success = JsonResponse.success(null, ResponseCodeEnum.LOGOUT.getMsg());
            success.setCode(ResponseCodeEnum.LOGOUT.getCode());
            return success;
        }
        JsonResponse allUnacceptedCoupons = couponService.getAllUnacceptedCoupons(user.getId());
        return allUnacceptedCoupons;
    }

    /**
     * 领取优惠券
     */
    @RequestMapping("/acceptCoupon")
    public JsonResponse acceptCoupon(Integer id, Integer categoryId) {
        return couponService.acceptCoupon(id, categoryId);
    }

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Coupon  coupon =  couponService.getById(id);
        return JsonResponse.success(coupon);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        couponService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateCoupon(Coupon  coupon) throws Exception {
        couponService.updateById(coupon);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Coupon
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(Coupon  coupon) throws Exception {
        couponService.save(coupon);
        return JsonResponse.success(null);
    }
}

